/*
This file is part of the replication package of "Asset-Price Redistribution" by
Fagereng, Gomez, Gouin-Bonenfant, Holm, Moll, and Natvik. 

This file produces a set of summary stats per year of transactions (flows) and holdings (stocks).  

*/

********************************************************************************

use lnr year stocks* debt* mfund *_pb net_private_debt bonds total_wealth financial_wealth human_wealth deposits housing *_total using $data/apr_orig_9319, clear

********************************************************************************
* AUXILIARY VARIABLES
********************************************************************************

tsset lnr year
gen private_equity = stocks_nonreg - deposits_pb - stocks_pb - housing_pb + debt_pb

gen public_equity = stocks + mfund + stocks_pb
replace debt = debt - net_private_debt - bonds

foreach var in housing debt deposits stocks {
	rename `var' `var'_dh
	gen `var' = `var'_dh + `var'_pb	
	}

replace financial_wealth = housing - debt + deposits + public_equity + private_equity
replace total_wealth = financial_wealth + human_wealth

	
********************************************************************************
* MAKE SUMMARY STATS OF TRANSACTIONS (FLOWS) EACH YEAR
********************************************************************************

forval jj = 1994 / 2019 {

	qui estpost summarize nt_housing_total nt_equity_total debt_total deposits_total if year == `jj', d
	esttab using ${output}/sum_stats_transactions_`jj'.csv, cells("mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99 count") csv replace noobs plain compress nomtitles

}

********************************************************************************
* MAKE SUMMARY STATS OF HOLDINGS (STOCKS) EACH YEAR
********************************************************************************

forval jj = 1993 / 1993 {

	qui estpost summarize total_wealth financial_wealth housing debt deposits public_equity private_equity human_wealth if year == `jj', d
	esttab using ${output}/sum_stats_stocks_`jj'.csv, cells("mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99 count") csv replace noobs plain compress nomtitles

}

